package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.pojos.WmNews;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


@Component
public class ArticleListener {
    @Autowired
    private ApArticleConfigMapper configMapper;
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = "update")
    public void onMessage(String message) {
        if (StringUtils.isNotBlank(message)){
            Map map = JSON.parseObject(message, Map.class);
            Object articleid = map.get("articleid");
            ApArticleConfig config = configMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleid));
            Object enable = map.get("enable");
            if (enable.equals(1)&&null!=enable){
                config.setIsDown(false);
                apArticleConfigService.updateById(config);
            }else {
                config.setIsDown(true);
                apArticleConfigService.updateById(config);
            }
        }

      /*  if (!StringUtils.isEmpty(message)) {
            //1.成功代码
      *//*      Map map = JSON.parseObject(message, Map.class);
            Object enable = map.get("enable");
            Boolean isDonw=true;
            if (enable.equals(1)){
                isDonw=false;
            }
            apArticleConfigService
                    .update(Wrappers.<ApArticleConfig>lambdaUpdate()
                            .eq(ApArticleConfig::getArticleId,map.get("articleid")).set(ApArticleConfig::getIsDown,isDonw));*//*
            //-----------------------
            //不成功代码
            Map map = JSON.parseObject(message, Map.class);
            Object articleid = map.get("articleid");
            ApArticleConfig apArticleConfig = configMapper.selectById((Serializable) articleid);//这一步是不是有问题???
            Boolean isdown=true;
            if (null != apArticleConfig) {
                if (map.get("enable").equals(1)) {
                    isdown = false;
                }
                apArticleConfig.setIsDown(isdown);
                configMapper.updateById(apArticleConfig);
            }
        }*/
    }
}